<?php

// +----------------------------------------------------------------------
// | ShintianCMS
// +----------------------------------------------------------------------
// | Copyright (c) 2011 Shintian All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: ljt <ljt1002@gmail.com>
// +----------------------------------------------------------------------
class OptionsAction extends BaseAction {

    /**
      +----------------------------------------------------------
     * 数据库与文件配置列表
      +----------------------------------------------------------
     * @access public
      +----------------------------------------------------------
     */
    function index() {
        $web_config = require APP_PATH . "/siteconfig.inc.php";
        foreach ($web_config as $key => $value) {
            if ($value === true)
                $web_config[$key] = "true";
            if ($value === false)
                $web_config[$key] = "false";
        }
        $this->assign("cache", array(
            "Cache" => array("Cache" => "./SCenter/Runtime/Cache", "Temp" => "./SCenter/Runtime/Temp", "Logs" => "./SCenter/Runtime/Logs", "Data" => "./SCenter/Runtime/Data"),
            "Runtime" => array("Runtime" => "./SCenter/Runtime")
        ));
        $this->assign("web_config", $web_config);
        $this->display();
    }

    /**
      +----------------------------------------------------------
     * 保存配置到数据库和config文件
      +----------------------------------------------------------
     * @access public
      +----------------------------------------------------------
     */
    function save() {
        //db_siteurl和EXTRA_UPLOAD_PATH这两个值如果用户在最后写上了/,把/去掉
        if (substr($_POST["SITE_HOST"], strlen($_POST["SITE_HOST"]) - 1) == '/')
            $_POST["SITE_HOST"] = substr($_POST["SITE_HOST"], 0, strlen($_POST["SITE_HOST"]) - 1);

        $web_config = "<?php\r\nif (!defined('THINK_PATH')) exit();\r\nreturn array(\r\n";
        foreach ($_POST as $key => $value) {
            if (strtolower($value) == "true" || strtolower($value) == "false" || is_numeric($value))
                $web_config .= "\t'" . $key . "'=>$value,\r\n";
            else
                $web_config .= "\t'" . $key . "'=>'$value',\r\n";
        }
        $web_config .= ");\r\n?>";

        file_put_contents (APP_PATH . "/siteconfig.inc.php", $web_config);
        $this->success(L('_OPTIONS_SAVE_SUCCESS_'));
    }

    function clearCache() {
        $dirs = $_POST['dir'];
        foreach ($dirs as $value) {
            $current_dir = @opendir($value);
            while ($entryname = readdir($current_dir)) {
                if (is_file($value . "/" . $entryname)) {
                    @unlink($value . "/" . $entryname);
                }
            }
            @closedir($current_dir);
        }
        $this->success(L('_OPTIONS_CLEARCACHE_SUCCESS_'));
    }

}

?>